package cn.timelost.hr.controller;

import cn.timelost.hr.config.utils.JWTUtils;
import cn.timelost.hr.config.utils.PageInfoUtil;
import cn.timelost.hr.config.utils.SysTime;
import cn.timelost.hr.dao.PersonalDao;
import cn.timelost.hr.pojo.Attendance;
import cn.timelost.hr.pojo.User;
import cn.timelost.hr.service.PersonalService;
import cn.timelost.hr.service.UserService;
import cn.timelost.hr.vo.PersonalDTO;
import cn.timelost.hr.vo.PersonalSelectVo;
import cn.timelost.hr.vo.PersonalVo;
import cn.timelost.hr.vo.ResultVo;
import cn.timelost.hr.vo.input.PersonalForm;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.sql.Date;
import java.util.List;
import java.util.Set;

/**
 * @author: liuxinqi
 * @Date: 2022/3/28 18:16
 */
@RestController
public class PersonalController {

    @Autowired
    PersonalService personalService;
    @Autowired
    UserService userService;
    @Resource
    PersonalDao personalDao;
    @GetMapping("/getPersonIdnow")
    public int findPersonIdnow(HttpServletRequest httpServletRequest){
        //
        String token = httpServletRequest.getHeader("Authorization");
        String username = JWTUtils.getUsername(token);
//        User user=userService.findByUsername(username);
//        Date date= new SysTime().getDate();
        System.out.println("HelloW1"+personalDao.findAllByName(username));
        try{
            return personalDao.findAllByName(username);
        }catch (Exception e){
            return -1;//没查到
        }
    }

    @GetMapping("/personal/list")
    public PageInfo<PersonalVo> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                     @RequestParam(value = "size", defaultValue = "10") Integer size,
                                     @RequestParam(value = "departmentId", defaultValue = "0") Integer departmentId,
                                     @RequestParam(value = "personalName", required = false) String personalName,
                                     @RequestParam(value = "workStatus", defaultValue = "0") Integer workStatus) {
        System.out.println("He01");
        System.out.println("He01"+personalService.findAll(page, size, departmentId, personalName, workStatus));
        System.out.println("He01"+personalService.findAllDTO(page, size, departmentId, personalName, workStatus));

        return personalService.findAll(page, size, departmentId, personalName, workStatus);
    }
    @GetMapping("/personal/listDTO")
    public PageInfo<PersonalDTO> listDTO(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                     @RequestParam(value = "size", defaultValue = "10") Integer size,
                                     @RequestParam(value = "departmentId", defaultValue = "0") Integer departmentId,
                                     @RequestParam(value = "personalName", required = false) String personalName,
                                     @RequestParam(value = "workStatus", defaultValue = "0") Integer workStatus) {
        System.out.println("He01");
        System.out.println("He01"+personalService.findAllDTO(page, size, departmentId, personalName, workStatus));

        return personalService.findAllDTO(page, size, departmentId, personalName, workStatus);
    }

    @GetMapping("/personal/all")
    public List<PersonalVo> all() {
        return personalService.All();
    }
    
    @GetMapping("/personal/select")
    public List<PersonalSelectVo> findSelect() {
        return personalService.findSelect();
    }

    @GetMapping("/personal/{id}")
    public PersonalVo findById(@PathVariable Integer id) {
        return personalService.find(id);
    }

    @GetMapping("/personal/getSolo")
    public PersonalVo getSolo(HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("Authorization");
        String username = JWTUtils.getUsername(token);
        int id=personalDao.findAllByName(username);
        System.out.println("hello789"+personalService.find(id));
        return personalService.find(id);
    }

    /**
     * 新建员工的时候给他注册账号，账号密码都先定为手机号吧，后面再说
     * 具体实现放在下一层了，
     * @param personalForm
     * @return
     */
    @PostMapping("/personal")
    @RequiresRoles("admin")
    public ResultVo insert(@RequestBody @Valid PersonalForm personalForm) {

        personalService.insert(personalForm);
        return ResultVo.success();
    }

    @PutMapping("/personal/{id}")
    @RequiresRoles("admin")
    public ResultVo update(@RequestBody PersonalForm personalForm, @PathVariable Integer id) {
        personalService.updateById(id, personalForm);
        return ResultVo.success();
    }

    @DeleteMapping("/personal/{id}")
    @RequiresRoles("admin")
    public ResultVo delete(@PathVariable Integer id) {
        personalService.deleteById(id);
        return ResultVo.success();
    }

    @DeleteMapping("/personal")
    @RequiresRoles("admin")
    public ResultVo deleteIdIn(@RequestBody Set<Integer> id) {
        personalService.deleteByIdIn(id);
        return ResultVo.success();
    }
}
